<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;

use app\common\library\Upload;
use think\Config;
use think\Db;

/**
 * 订单接口 12
 */
class Order extends Api
{

    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $ordersn=$this->request->param('ordersn');



          $order= Db::name('order')
              ->where(['ordersn'=>$ordersn])
              ->where('status','eq',0)
              ->find();



        if (!$order){
            $this->error('没有订单信息'.$ordersn);
        }


    }



    /**
     * 上传文件
     * @ApiMethod (POST)
     * @param File $file 文件流
     */
    public function uploadone()
    {
        Config::set('default_return_type', 'json');
        //必须设定cdnurl为空,否则cdnurl函数计算错误
        Config::set('upload.cdnurl', '');


        $ordersn=$this->request->param('ordersn');


        //默认普通上传文件
        $file = $this->request->file('file');
        try {
            $upload = new Upload($file);
            $attachment = $upload->upload('/uploads/one/'.$ordersn.'/'.uniqid().'.png');
        } catch (UploadException $e) {
            $this->error($e->getMessage());
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);


    }



    /**
     * 上传文件
     * @ApiMethod (POST)
     * @param File $file 文件流
     */
    public function uploadtwo()
    {



        $fileName = $_FILES['file'];


        $tmpFilePath = $_FILES['file']['tmp_name'];


        //文件夹目录 12321
        $dirPath='/uploads/2024/'.uniqid().'.png';




        $targetDir = ROOT_PATH.'public'.$dirPath;  // 指定目录 2222


        move_uploaded_file($tmpFilePath, $targetDir);


        $this->success(__('Uploaded successful'), ['url' => $dirPath, 'fullurl' => cdnurl($dirPath, true)]);


        die;

        Config::set('default_return_type', 'json');
        //必须设定cdnurl为空,否则cdnurl函数计算错误 111
        Config::set('upload.cdnurl', '');


        $ordersn=$this->request->param('ordersn');


        //默认普通上传文件
        $file = $this->request->file('file');



        try {
            $upload = new Upload($file);
            $attachment = $upload->upload('/uploads/two/'.$ordersn.'/'.uniqid().'.png');
        } catch (UploadException $e) {
            $this->error($e->getMessage());
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);


    }


    public function editorder(){

        $ordersn=$this->request->param('ordersn');

        $imgs=$this->request->param('images');

        $types=$this->request->param('types');


        if (!$imgs){
            $this->error('未提交内容');
        }


        $log['images']=$imgs;
        $log['status']=1;
        $log['typeslist']=$types?$types:2;


        Db::name('order')->where(['ordersn'=>$ordersn])->update($log);


        $this->success('提交成功！');

    }






}